package com.ramos.kuaixiao.shops.ui;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.ui.BaseActivity;
import com.ramos.kuaixiao.utils.DownloadImageTask;
import com.ramos.kuaixiao.utils.StartLocation;
import com.ramos.kuaixiao.utils.ToastUtils;

public class MallMainActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = "MainActivity";
    // 定义FragmentManager对象管理器
    private FragmentManager fragmentManager;
    private RelativeLayout re_home_shop;
    private RelativeLayout re_home_order;
    private RelativeLayout re_home_task;
    private RelativeLayout re_home_my;
    private MallProductFragment mMallProductFragment;
    private MallOrderFragment mMallOrderFragment;
    private MallShopFragment mMallShopFragment;
    private MallUserFragment mMallUserFragment;

    private TextView tv_home_shop;
    private TextView tv_home_order;
    private TextView tv_home_task;
    private TextView tv_home_my;
    private ImageView ib_home_shop;
    private ImageView ib_home_order;
    private ImageView ib_home_task;
    private ImageView ib_home_my;
    private long exitTime = 0;
    StartLocation startLocation;
    private LocationManager lm;//【位置管理】
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkGPS();
        startLocation = new StartLocation(this);
        startLocation.startLocation();
        setContentView(R.layout.activity_main);
        initView();
        getPermission();
    }

    private void getPermission() {
         if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                ) {
            //就跳到让用户选择是否授权  给个码走返回方法
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA
                            ,Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);
        }
    }

    private void checkGPS() {
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (ok) {//开了定位服务
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                // 没有权限，申请权限。
                //Toast.makeText(getActivity(), "没有权限", Toast.LENGTH_SHORT).show();

            } else {
                // 有权限了，去放肆吧。
                // Toast.makeText(getActivity(), "有权限", Toast.LENGTH_SHORT).show();
            }
        } else {
            //Toast.makeText(getActivity(), "系统检测到未开启GPS定位服务", Toast.LENGTH_SHORT).show();
            ToastUtils.show("系统检测到未开启GPS定位服务");
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(intent, 1315);
        }
    }

    //当我申请权限的时候,调用
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //如果用户选择了同意授权     走我的方法
        if (requestCode == 1001) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            } else {
                ToastUtils.show("权限拒绝");
            }
        }
    }


    private void initView() {
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.title_mall), false);
        fragmentManager = getSupportFragmentManager();
        re_home_shop = (RelativeLayout) findViewById(R.id.re_home_shop);
        re_home_order = (RelativeLayout) findViewById(R.id.re_home_order);
        re_home_task = (RelativeLayout) findViewById(R.id.re_home_task);
        re_home_my = (RelativeLayout) findViewById(R.id.re_home_my);
        re_home_shop.setOnClickListener(this);
        re_home_order.setOnClickListener(this);
        re_home_task.setOnClickListener(this);
        re_home_my.setOnClickListener(this);

        ib_home_shop = ((ImageView) findViewById(R.id.ib_home_shop));
        ib_home_order = ((ImageView) findViewById(R.id.ib_home_order));
        ib_home_task = ((ImageView) findViewById(R.id.ib_home_task));
        ib_home_my = ((ImageView) findViewById(R.id.ib_home_my));

        tv_home_shop = ((TextView) findViewById(R.id.tv_home_shop));
        tv_home_shop.setText("商品");
        tv_home_order = ((TextView) findViewById(R.id.tv_home_order));
        tv_home_task = ((TextView) findViewById(R.id.tv_home_task));
        tv_home_my = ((TextView) findViewById(R.id.tv_home_my));

        showShop();
        new DownloadImageTask().execute();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        startLocation.stotLocation();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.re_home_shop:
                showShop();
                break;
            case R.id.re_home_order:
                showOrder();
                break;
            case R.id.re_home_task:
                showTask();
                break;
            case R.id.re_home_my:
                showMy();
                break;
            default:
                break;
        }
    }

    private void showTask() {
        ib_home_shop.setImageResource(R.drawable.tab_infomation);
        tv_home_shop.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_order.setImageResource(R.drawable.tab_schedule);
        tv_home_order.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_task.setImageResource(R.drawable.tab_service_clk);
        tv_home_task.setTextColor(getResources().getColor(R.color.title));

        ib_home_my.setImageResource(R.drawable.tab_contacts);
        tv_home_my.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        showFragment("Task");

    }

    private void showMy() {
        ib_home_shop.setImageResource(R.drawable.tab_infomation);
        tv_home_shop.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_order.setImageResource(R.drawable.tab_schedule);
        tv_home_order.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_task.setImageResource(R.drawable.tab_service);
        tv_home_task.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_my.setImageResource(R.drawable.tab_contacts_clk);
        tv_home_my.setTextColor(getResources().getColor(R.color.title));

        showFragment("My");
    }

    private void showShop() {
        ib_home_shop.setImageResource(R.drawable.tab_infomation_clk);
        tv_home_shop.setTextColor(getResources().getColor(R.color.title));

        ib_home_order.setImageResource(R.drawable.tab_schedule);
        tv_home_order.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_task.setImageResource(R.drawable.tab_service);
        tv_home_task.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_my.setImageResource(R.drawable.tab_contacts);
        tv_home_my.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        showFragment("Shop");
    }

    private void showOrder() {
        ib_home_shop.setImageResource(R.drawable.tab_infomation);
        tv_home_shop.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_order.setImageResource(R.drawable.tab_schedule_clk);
        tv_home_order.setTextColor(getResources().getColor(R.color.title));

        ib_home_task.setImageResource(R.drawable.tab_service);
        tv_home_task.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        ib_home_my.setImageResource(R.drawable.tab_contacts);
        tv_home_my.setTextColor(getResources().getColor(R.color.bottom_tab_unselected));

        showFragment("Order");
    }

    private void showFragment(String paramString) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        switch (paramString) {
            case "Shop":
                // 如果fg1为空，则创建一个并添加到界面上

                if (mMallProductFragment == null) {
                    mMallProductFragment = new MallProductFragment();
                    fragmentTransaction.add(R.id.fragment_container, mMallProductFragment);
                } else {
                    // 如果不为空，则直接将它显示出来
                    fragmentTransaction.show(mMallProductFragment);
                }
                break;
            case "Order":
                // 如果fg2为空，则创建一个并添加到界面上
                if (mMallOrderFragment == null) {
                    mMallOrderFragment = new MallOrderFragment();
                    fragmentTransaction.add(R.id.fragment_container, mMallOrderFragment);
                } else {
                    // 如果不为空，则直接将它显示出来
                    fragmentTransaction.show(mMallOrderFragment);
                }
                break;

            case "Task":
                // 如果fg2为空，则创建一个并添加到界面上
                if (mMallShopFragment == null) {
                    mMallShopFragment = new MallShopFragment();
                    fragmentTransaction.add(R.id.fragment_container, mMallShopFragment);
                } else {
                    // 如果不为空，则直接将它显示出来
                    fragmentTransaction.show(mMallShopFragment);
                }
                break;

            case "My":
                // 如果fg4为空，则创建一个并添加到界面上
                if (mMallUserFragment == null) {
                    mMallUserFragment = new MallUserFragment();
                    fragmentTransaction.add(R.id.fragment_container, mMallUserFragment);
                } else {
                    // 如果不为空，则直接将它显示出来
                    fragmentTransaction.show(mMallUserFragment);
                }
                break;
        }
        fragmentTransaction.commit(); // 提交
    }

    /**
     * 隐藏Fragment
     */
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        if (mMallProductFragment != null) {
            fragmentTransaction.hide(mMallProductFragment);
        }
        if (mMallOrderFragment != null) {
            fragmentTransaction.hide(mMallOrderFragment);
        }
        if (mMallShopFragment != null) {
            fragmentTransaction.hide(mMallShopFragment);
        }
        if (mMallUserFragment != null) {
            fragmentTransaction.hide(mMallUserFragment);
        }
    }

   @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
